/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package uk.surrey.ash.ga;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author Ashish
 *
 */
public class Codebook {
	private int[] histogramPositive;
	private int[] histogramNegative;
	private double codebookFitness;
	/**
	 * @param numberOfGenes
	 */
	public Codebook(int numberOfGenes) {
		setHistogramPositive(new int[numberOfGenes]);
		setHistogramNegative(new int[numberOfGenes]);
		codebookFitness = 0;
	}
	/**
	 * 
	 */
	public Codebook() {
	}
	/**
	 * @param codebookFitness the codebookFitness to set
	 */
	public void computeCodebookFitness() {
		for(int i=0;i<histogramPositive.length;i++){
			codebookFitness += Math.sqrt(histogramPositive[i])*Math.sqrt(histogramNegative[i]);
		}
	}
	/**
	 * @return the codebookFitness
	 */
	public double getCodebookFitness() {
		return codebookFitness;
	}
	
	/**
	 * @param pop
	 * @param cellspace 
	 */
	public void buildCodebook(Population pop, CellSpace cellspace) {
//		build the histogram for positive images
		
	}
	/**
	 * @param chromosome 
	 */
	public void setHistogramPositive(ArrayList<Gene> chromosome) {
		Iterator<Gene> geneItr = chromosome.iterator();
		while(geneItr.hasNext()){			
		}
	}
	
	/**
	 * @param randomsamples
	 * @param i
	 * @param cellspace
	 */
	public void scoreHistograms(long[] randomsamples, int geneIdx, CellSpace cellspace) {
//		convert random samples from long to int
		for(int i=0;i<randomsamples.length;i++){
			int cellclass = cellspace.getCells().get((int)randomsamples[i]).getImageClass();
			if(cellclass == +1){
				histogramPositive[geneIdx]++;
			}
			else if(cellclass == -1){
				histogramNegative[geneIdx]++;
			}
			else{
//				NOOP
			}
		}
	}
	/**
	 * @param histogramPositive the histogramPositive to set
	 */
	public void setHistogramPositive(int[] histogramPositive) {
		this.histogramPositive = histogramPositive;
	}
	/**
	 * @return the histogramPositive
	 */
	public int[] getHistogramPositive() {
		return histogramPositive;
	}
	/**
	 * @param histogramNegative the histogramNegative to set
	 */
	public void setHistogramNegative(int[] histogramNegative) {
		this.histogramNegative = histogramNegative;
	}
	/**
	 * @return the histogramNegative
	 */
	public int[] getHistogramNegative() {
		return histogramNegative;
	}
	/**
	 * @param geneIdx
	 * @param cellclass
	 */
	public void addCell(int geneIdx, int cellclass) {
		if(cellclass == +1){
			histogramPositive[geneIdx]++;
		}
		else if(cellclass == -1){
			histogramNegative[geneIdx]++;
		}
		else{
//			NOOP
		}
	}
	/**
	 * @param chromosome
	 */
	public void scoreHistograms(Gene[] chromosome) {
		for(int i=0;i<chromosome.length;i++){
			Iterator<Cell> geneItr = chromosome[i].getGene().iterator();
			while(geneItr.hasNext()){
				int cellclass = geneItr.next().getImageClass();
				if(cellclass == +1){
					histogramPositive[i]++;
				}
				else if(cellclass == -1){
					histogramNegative[i]++;
				}
				else{
//					NOOP
				}
			}
		}
	}
	
}
